package org.android.brasil.projetos.oktoberfest2011.view;

import org.android.brasil.projetos.oktoberfest2011.R;
import org.android.brasil.projetos.oktoberfest2011.utils.AnalyticsUtils;
import org.android.brasil.projetos.oktoberfest2011.utils.Constantes;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

public class CameraTabView extends TabActivity {

	private AnalyticsUtils mGa;
	private boolean mudar = true;
	private TabHost tabHost;
	private static String[] cameras = new String[] { Constantes.SETOR1,
			Constantes.SETOR2, Constantes.SETOR3 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.mGa = AnalyticsUtils.getInstance(this);

		setContentView(R.layout.camera_tab_view);

		bindScreenFields();
		populateFields();

		this.mGa.trackScreen("Câmera");
	}

	private void bindScreenFields() {
		tabHost = getTabHost();
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			
			public void onTabChanged(String arg0) {
				if(!mudar){
					mudar = false;
					populateFields();
					mudar = true;
				}
				
			}
		});
	}

	private void populateFields() {
		Intent intent = null;
		Integer[] draw = new Integer[] { R.drawable.ic_cam_externa,
				R.drawable.ic_cam_setor1, R.drawable.ic_cam_setor2 };

		int index = tabHost.getCurrentTab();

		tabHost.setCurrentTab(0);
		tabHost.clearAllTabs();

		int i = 0;
		for (String cam : cameras) {
			TabHost.TabSpec spec;
			intent = new Intent().setClass(this, CameraView.class);
			if (getIntent().getExtras() != null) {
				intent.putExtras(getIntent().getExtras());
			}
			intent.putExtra("CAMERA", cam);
			spec = tabHost.newTabSpec(cam)
					.setIndicator("", getResources().getDrawable(draw[i]))
					.setContent(intent);
			i = i + 1;
			tabHost.addTab(spec);
		}
		tabHost.setCurrentTab(index);
	}

	protected void onDestroy() {
		super.onDestroy();
	}
}
